﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using T.Personnel.Domain;
using T.Personnel.Domain.Dto;
using T.Personnel.Repository.Interface;

namespace T.Personnel.Repository.Impl
{
    public class BaseRegisterRepository : BaseRepository<Register>, IRegisterRepository
    {
        private readonly EFDbcontext db;
        public BaseRegisterRepository(EFDbcontext eFDbcontext, EFDbcontext db) : base(eFDbcontext)
        {
            this.db = db;
        }

        public async Task<int> AddRegisterAsync(Register register)
        {
            return await base.AddAsync(register);
        }

        public async Task<int> DelRegisterAsync(int id,string name)
        {
            var allor = await base.GetAsync(id);
            allor.DeletedBy = name;
            allor.DeletedDate = DateTime.Now;
            allor.IsDeleted = true;
            return await base.UpdAsync(allor);
        }

        public async Task<List<Register>> GetAllAsync()
        {
            var gather = await base.GetListAsync(x => x.IsDeleted == false && x.State != "已离职");
            return gather.ToList();
        }

        public async Task<List<ItemsDto>> GetPrincipalsAsync(int Department, int StaffId)
        {
            var gather = await base.GetListAsync(x => x.DepartmentId.Equals(Department) && x.Id != StaffId && x.IsDeleted == false && x.State == "已转正");

            var result = gather.Select(item => new ItemsDto
            {
                value = item.Id,
                label = item.Name
            }).ToList();

            return result;
        }

        public async Task<Result<Register>> GetRegisterAsync(string? Name, int? DepartmentId, int? PositionId, string? Phone, string? EmployeeType, int PageIndex, int PageSize)
        {
            var gather = await base.GetListAsync(x => x.IsDeleted == false);
            if (Name != null)
            {
                gather = gather.Where(x => x.Name.Contains(Name));
            }
            if (DepartmentId != null)
            {
                gather = gather.Where(x => x.DepartmentId.Equals(DepartmentId));
            }
            if (PositionId != null)
            {
                gather = gather.Where(x => x.PositionId.Equals(PositionId));
            }
            if (Phone != null)
            {
                gather = gather.Where(x => x.Phone.Contains(Phone));
            }
            if (EmployeeType != null)
            {
                gather = gather.Where(x => x.EmployeeType.Equals(EmployeeType));
            }

            var totalCount = gather.Count();
            var totalPage = (int)Math.Ceiling(totalCount * 1.0 / PageSize);
            var list = gather.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
            Result<Register> result = new Result<Register>();
            result.TotalCount = totalCount;
            result.TotalPage = totalPage;
            result.Data = list;
            return result;
        }

        public async Task<Register> GetRegisterAsync(int id)
        {
            return await base.GetAsync(id);
        }

        public async Task<List<ItemsDto>> GetUserEntryAsync(int PositionId)
        {
            var gather = await base.GetListAsync(x => x.PositionId.Equals(PositionId) && x.IsDeleted == false && x.State == "待入职");

            var result = gather.Select(item => new ItemsDto
            {
                value = item.Id,
                label = item.Name
            }).ToList();

            return result;
        }

        public async Task<List<ItemsDto>> GetUserFullAsync(int PositionId)
        {
            var gather = await base.GetListAsync(x => x.PositionId.Equals(PositionId) && x.IsDeleted == false && x.State == "试用期");

            var result = gather.Select(item => new ItemsDto
            {
                value = item.Id,
                label = item.Name
            }).ToList();

            return result;
        }

        public async Task<List<ItemsDto>> GetUserResignationAsync(int PositionId)
        {
            var gather = await base.GetListAsync(x => x.PositionId == PositionId && x.IsDeleted == false && (x.State == "试用期" || x.State == "已转正"));

            var result = gather.Select(item => new ItemsDto
            {
                value = item.Id,
                label = item.Name
            }).ToList();

            return result;
        }

        public async Task<int> UpdRegisterAsync(Register register)
        {
            return await base.UpdAsync(register);
        }
    }
}
